﻿namespace GGEditor.API.Utility.Algorithms
{
    public static class Map
    {
        // http://www.codeproject.com/KB/cs/perlopcs.aspx

        #region Delegates

        public delegate void Action();

        public delegate T Action<T>(T item);

        public delegate DstType Action<SrcType, DstType>(SrcType item);

        #endregion

        public static T[] ToArray<T>(T[] source, Action<T> action)
        {
            Contract.NotNull(source, "source");
            Contract.NotNull(action, "action");

            T[] result = new T[source.Length];
            for (int i = 0; i < source.Length; ++i)
                result[i] = action(source[i]);
            return result;
        }

        public static DstType[] ToArray<SrcType, DstType>(
            SrcType[] source, Action<SrcType, DstType> action)
        {
            Contract.NotNull(source, "source");
            Contract.NotNull(action, "action");

            DstType[] result = new DstType[source.Length];
            for (int i = 0; i < source.Length; ++i)
                result[i] = action(source[i]);
            return result;
        }
    }
}